<!DOCTYPE html>
<html lang="zh">
<head>
	<meta charset="utf-8">
    <title>XDebug小助手</title>
	<style type="text/css">
		.btn{
			width:100%;
			height:50px;
			font-size:18px;
		}
		.current{
			border:3px solid #0f0;
		}
	</style>
 </head>
 <body>
 	<div class="btn_list"></div>
	<script type="text/javascript" src="http://tool.oschina.net/js/jquery/jquery-1.7.2.js"></script>
	<script>
		var now_mode = "disable";
		var time = 365 * 24 * 60 * 60 * 1000;
		var debug_Object = new Object();
		debug_Object.debug = {
			default_status:"",
			cookie_name:"XDEBUG_SESSION",
			cookie_value:"PHPSTORM",
		};
		debug_Object.profile = {
			default_status:"",
			cookie_name:"XDEBUG_PROFILE",
			cookie_value:"XDEBUG_ECLIPSE"
		};
		debug_Object.trace = {
			default_status:"",
			cookie_name:"XDEBUG_TRACE",
			cookie_value:"XDEBUG_ECLIPSE"
		};
		debug_Object.disable = {
			default_status:"current",
			cookie_name:"DISABLE",
			cookie_value:null
		};
		$(function(){
			init();
			check();
			$(".btn").click(btnClick);
		});
		function btnClick(){
			var mode = $(this).attr("id");
			if(mode==now_mode){
				return;
			}
			change2Mode(mode);
		}
		function change2Mode(to_mode){
			changeCookie(now_mode,to_mode);
			changeDisplay(now_mode,to_mode);
			now_mode = to_mode;
		}
		function check(){
			//console.log("check_start");
			var check_mode = "";
			for(var mode in debug_Object){
				var value = debug_Object[mode];
				//console.log(value.cookie_name);
				var cookie_value = getCookie(value.cookie_name);
				//console.log(cookie_value);
				if(cookie_value==value.cookie_value){
					check_mode = mode;
					break;
				}else if(typeof cookie_value=='string' && cookie_value!=''){
					check_mode = mode;
					break;
				}
			}
			if(check_mode!=""){
				changeDisplay(now_mode,check_mode);
				now_mode = check_mode;
			}
			//console.log("check_end");
		}
		function changeDisplay(fromMode,to_mode){
			$("#"+fromMode).removeClass("current");
			$("#"+to_mode).addClass("current");
		}
		function changeCookie(fromMode,to_mode){
			setCookie(debug_Object[fromMode].cookie_name,null,-1);
			setCookie(debug_Object[to_mode].cookie_name,debug_Object[to_mode].cookie_value,time);
		}
		function init(){
			for(var mode in debug_Object){
				var value = debug_Object[mode];
				//console.log(mode);
				var item_str = '<button class="btn" id="'+mode+'">'+mode.toLocaleUpperCase()+'</button><br/><br/>';
				$(".btn_list").append(item_str);
				if(value.default_status=="current"){
					now_mode = mode;
					$("#"+mode).addClass("current");
				}
			}
		}
		function setCookie(cookieName,value,expiredays){
			var exdate=new Date()
			exdate.setDate(exdate.getDate()+expiredays)
			document.cookie=cookieName+ "=" +escape(value)+
					((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
		}
		function getCookie(cookieName){
			if (document.cookie.length>0)
			{
				c_start=document.cookie.indexOf(cookieName + "=")
				if (c_start!=-1)
				{
					c_start=c_start + cookieName.length+1
					c_end=document.cookie.indexOf(";",c_start)
					if (c_end==-1) c_end=document.cookie.length
					return unescape(document.cookie.substring(c_start,c_end))
				}
			}
			return ""
		}
		function checkCookie(){
			username=getCookie('username')
			if (username!=null && username!="")
			{alert('Welcome again '+username+'!')}
			else
			{
				username=prompt('Please enter your name:',"")
				if (username!=null && username!="")
				{
					setCookie('username',username,365)
				}
			}
		}
	</script>
 </body>
</html>
